<?php
header('Content-Type: text/plain');
require_once dirname(__FILE__) . '/../../../incls/php/dbutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/miscutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/config.php';
session_name(SESSION);
session_start();
$myPdo = DbUtils::createPdoInst();
$action = $_POST['action'];
try {
  $myPdo->beginTransaction();
  switch ($action) {
    case 'save_customer':
      save_customer($myPdo);
      break;
    case 'save_addr':
      save_addr($myPdo);
      break;
    case 'set_default':
      set_default($myPdo);
      break;
    case 'del_addr':
      del_addr($myPdo);
      break;
    case 'check_account':
      check_account($myPdo);
      break;
    default:
      break;
  }
  $myPdo->commit();
} catch (PDOException $e) {
  echo $e->getMessage();
}

function save_customer($myPdo) {
  if (!isset($_SESSION['admin' . USER])) {
    return;
  }
  $q = json_decode(MiscUtils::getParam('q', '[]'));
  
  if (isset($q->id)) {
    if (isset($q->_password)) {
      $q->password = md5($q->_password);
    }
    DbUtils::update($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_customer', $q);
  } else {
  	$q->password = md5($q->password);
  	$q->reg_date = date('Y-m-d', time());
    DbUtils::add($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_customer', $q);
    $q->id = $myPdo->lastInsertId();
  }
  
  if (isset($q->_id)) {
    $myPdo->exec("UPDATE "  . TABLE_NAME_PREFIX_PLUGIN . "ecom_customer_addr SET customer_id=$q->id WHERE customer_id=$q->_id");
  }
  
  echo json_encode($q);
}

function save_addr($myPdo) {
  if (!isset($_SESSION['admin' . USER])) {
    return;
  }
  
  $q = json_decode(MiscUtils::getParam('q', '[]'));
  
  if ($q->is_default == 1) {
    $myPdo->exec("UPDATE " . TABLE_NAME_PREFIX_PLUGIN . "ecom_customer_addr SET is_default = 0 WHERE customer_id=$q->customer_id");
  }
  
  if (isset($q->id)) {
    DbUtils::update($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_customer_addr', $q);
  } else {
    DbUtils::add($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_customer_addr', $q);
  }
  
  $cond_vals = new stdClass();
  $cond_vals->c = 't.customer_id = :v1';
  $cond_vals->v = array(':v1' => $q->customer_id);
  $items = DbUtils::get($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_customer_addr', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
  
  echo json_encode($items);
}

function set_default($myPdo) {
  if (!isset($_SESSION['admin' . USER])) {
    return;
  }
  $q = json_decode(MiscUtils::getParam('q', '[]'));
  
  $myPdo->exec('UPDATE ' . TABLE_NAME_PREFIX_PLUGIN . 'ecom_customer_addr SET is_default = 0 WHERE customer_id = '. $q->customer_id);

  $q->is_default = 1;
  DbUtils::update($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_customer_addr', $q);
  
  $cond_vals = new stdClass();
  $cond_vals->c = 't.customer_id = :v1';
  $cond_vals->v = array(':v1' => $q->customer_id);
  $items = DbUtils::get($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_customer_addr', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
  
  echo json_encode($items);
}

function del_addr($myPdo) {
  if (!isset($_SESSION['admin' . USER])) {
    return;
  }
  $q = json_decode(MiscUtils::getParam('q', '[]'));
  $customer_id = $q->customer_id;
 
  $cond_vals = new stdClass();
  $cond_vals->c = 'id = :v1';
  $cond_vals->v = array(':v1' => $q->id);
  DbUtils::deleteByCond($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'ecom_customer_addr', $cond_vals);
  
  $cond_vals = new stdClass();
  $cond_vals->c = 't.customer_id = :v1';
  $cond_vals->v = array(':v1' => $customer_id);
  $tmps = DbUtils::get($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_customer_addr', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
  if ($q->is_default == 1 && count($tmps) > 0) {
  	$tmps[0]->is_default = 1;
    DbUtils::update($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_customer_addr', $tmps[0]);
  }
  
  $cond_vals = new stdClass();
  $cond_vals->c = 't.customer_id = :v1';
  $cond_vals->v = array(':v1' => $customer_id);
  $items = DbUtils::get($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_customer_addr', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
  
  echo json_encode($items);
}

function check_account($myPdo) {
  if (!isset($_SESSION['admin' . USER])) {
    return;
  }
  //
  $q = MiscUtils::getParam('q', NULL);
  $cond_vals = new stdClass();
  $cond_vals->c = 't.account = :v1';
  $cond_vals->v = array(':v1' => $q);
  $tmp = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'ecom_customer', $cond_vals, NULL, NULL, NULL, NULL, NULL);
  if (count($tmp->d) == 1) {
    echo json_encode(array( 'result' => 0 ));
    return;
  }
  echo json_encode(array( 'result' => 1 ));
}

?>